<?php

namespace addons\qingdongams\controller;

use addons\qingdongams\model\Order as OrderModel;
use addons\qingdongams\model\StaffSignIn;
use addons\qingdongams\model\Staff;
use addons\qingdongams\model\Contract;

/**
 * 订单管理
 */
class Order extends StaffApi
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = [];


    //获取订单记录
    public function getList() {

        $params = $this->request->post();
        $limit  = input("limit/d", 10);
        $staff_id  = input("staff_id", '');
        $number  = input("number", '');

        $where  = [];
        if($staff_id){
            $where['create_staff_id']=$staff_id;
        }else{
            $where['create_staff_id'] = ['in',Staff::getMyStaffIds()];
        }
        if($number){
            $ids=Contract::where(['num'=>['like',"%{$number}%"]])->column('id');
            $where['contract_id']=['in',$ids];
        }
        $order  = 'id desc';

        if(isset($params['id_list'])){//日志 查询id列表
            $where=[];
            $where['id']=['in',explode(',',$params['id_list'])];
        }
        $list              = OrderModel::where($where)->with([
            'customer','contract'
        ])->order($order)->paginate($limit);
        $this->success('请求成功', $list);
    }


    //获取订单详情
    public function getDetail(){
        $id=input('id');
        $order=OrderModel::where(['id'=>$id])->with(['customer','contract','product'])->find();
        if(empty($order)){
            $this->error('订单信息不存在');
        }
        $order['is_operation']=0;

        if($order['owner_staff_id'] == $this->auth->id){
            //是否可以操作
            $order['is_operation']=1;
        }

        $order['record']=StaffSignIn::where(['relation_type'=>StaffSignIn::ORDER_TYPE,'relation_id'=>$id])
            ->field('id,staff_id,relation_type,relation_id,content,file_ids,createtime,staff_id')->with(['staff'])->select();
        $this->success('请求成功',$order);
    }
    //录入到账信息
    public function inputPayment(){
        $id=input('id');
        $payment_date=input('payment_date');
        $file_ids=input('file_ids','');
        $remarks=input('remarks','');

        if(empty($payment_date)){
            $this->error('参数不能为空');
        }
        //存在通话内容
        $content='到账时间:'.$payment_date.'<br>备注:'.$remarks;
        $data = [
            'staff_id' => $this->auth->id,
            'relation_type' => StaffSignIn::ORDER_TYPE,//日程类型
            'relation_id' => $id,//签到关联id
            'content' => $content,//处理结果
            'file_ids' => $file_ids,
        ];
        StaffSignIn::createSignIn($data);

        $this->success('录入成功');
    }

}
